package com.omesoft.hksnore.util.json;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Message;
import android.util.Base64;
import android.util.Log;


import com.omesoft.hksnore.R;
import com.omesoft.hksnore.util.dao.FamilyIfc;
import com.omesoft.hksnore.util.dao.FamilyIfcImpl;
import com.omesoft.hksnore.util.data.MySharedPreferencesUtil;
import com.omesoft.hksnore.util.dialog.MyProgressBarDialogUtil;
import com.omesoft.hksnore.util.entity.FamilyDTO;
import com.omesoft.hksnore.util.photo.DownPhotoUtil;
import com.omesoft.hksnore.util.photo.MyBitmapUtil;
import com.omesoft.hksnore.util.thread.MyThread;
import com.omesoft.hksnore.util.web.StreamUtil;
import com.omesoft.hksnore.util.web.WebServiceUtils;

import org.json.JSONObject;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.HashMap;

import static com.omesoft.hksnore.util.constant.CusConstant.APPID;


/**
 * Created by Omesoft on 2017/2/21.
 */

public class SnorekillerJsonUtil {


    public static final String TAG = "SnorekillerJsonUtil";


    /**
     * 登录
     */

    public static void Login(final Handler handler, final Context context, final String username, final String password, final String ip, final String language) {

        try {

            MyThread.startNewThread(new Runnable() {
                @Override
                public void run() {
                    try {
                        HashMap<String, Object> PARAMS = new HashMap<String, Object>();
                        PARAMS.put("username", username);
                        PARAMS.put("password", password);
                        PARAMS.put("loginIP", ip);
                        PARAMS.put("appId", APPID);
                        PARAMS.put("language", language);
                        Log.i(TAG, "Login params  " + PARAMS.toString());
                        String resultStr = WebServiceUtils.callDotNetWS("Login", PARAMS);
                        Log.d(TAG, "Login  resultStr" + resultStr);
                        if (resultStr != null) {
                            try {
                                JSONObject temp = new JSONObject(resultStr);
                                int ret = temp.getInt("ret");
                                int code = temp.getInt("err_code");
                                String msg = temp.getString("msg");
                                if (ret == 0) {

                                    JSONObject jsonObject = temp.getJSONObject("data");
                                    MySharedPreferencesUtil.setMemberId(context, jsonObject.getInt("id"));
                                    MySharedPreferencesUtil.setCilenKey(context, jsonObject.getString("clientKey"));
                                    GetMemberInfo(handler, context, jsonObject.getInt("id"), jsonObject.getString("clientKey"));
                                } else {
                                    sendMsg(handler, ReErrorCode.ERRCODE_FAIL, msg);
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                                sendMsg(handler, ReErrorCode.ERRCODE_NO_RETRUN_ERROR, "");
                            }
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                        sendMsg(handler, ReErrorCode.ERRCODE_NO_RETRUN_ERROR, "");
                    }
                }
            });

        } catch (Exception e) {
            e.printStackTrace();
            sendMsg(handler, ReErrorCode.ERRCODE_NO_RETRUN_ERROR, "");
        }
    }

    /**
     * 短信验证登录
     */
    public static void LoginWithoutRegister(final Handler handler, final Context context, final String phoneNumber, final String verifyCode,  final String language) {

        try {
            MyProgressBarDialogUtil.show(context, R.string.login_loading);
            MyThread.startNewThread(new Runnable() {
                @Override
                public void run() {
                    try {
                        HashMap<String, Object> PARAMS = new HashMap<String, Object>();
                        PARAMS.put("phoneNumber", phoneNumber);
                        PARAMS.put("verifyCode", verifyCode);
                        PARAMS.put("appId", APPID);
                        PARAMS.put("language", language);
                        Log.i(TAG, "LoginWithoutRegister params  " + PARAMS.toString());
                        String resultStr = WebServiceUtils.callDotNetWS("LoginWithoutRegister", PARAMS);
                        Log.d(TAG, "LoginWithoutRegister  resultStr" + resultStr);
                        if (resultStr != null) {
                            try {
                                try {
                                    JSONObject temp = new JSONObject(resultStr);
                                    int ret = temp.getInt("ret");
                                    int code = temp.getInt("err_code");
                                    String msg = temp.getString("msg");
                                    if (ret == 0) {

                                        JSONObject jsonObject = temp.getJSONObject("data");
                                        MySharedPreferencesUtil.setMemberId(context, jsonObject.getInt("id"));
                                        MySharedPreferencesUtil.setCilenKey(context, jsonObject.getString("clientKey"));

                                        GetMemberInfo(handler, context, MySharedPreferencesUtil.getMemberId(context), MySharedPreferencesUtil.getCilenKey(context));
                                    } else {
                                        sendMsg(handler, ReErrorCode.ERRCODE_FAIL, msg);
                                    }
                                } catch (Exception e) {
                                    e.printStackTrace();
                                    sendMsg(handler, ReErrorCode.ERRCODE_NO_RETRUN_ERROR, "");
                                }
                            } catch (Exception e) {
                                sendMsg(handler, ReErrorCode.ERRCODE_NO_RETRUN_ERROR, "");
                            }
                        }else{
                            sendMsg(handler, ReErrorCode.ERRCODE_NO_RETRUN_ERROR, "");
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                        sendMsg(handler, ReErrorCode.ERRCODE_NO_RETRUN_ERROR, "");
                    }
                }
            });

        } catch (Exception e) {
            e.printStackTrace();
            sendMsg(handler, ReErrorCode.ERRCODE_NO_RETRUN_ERROR, "");
        }

    }
    /**
     *短信登录获取验证码
     */

    public static void GetSMSVerifyCode(final Handler handler, final Context context, final String phone) {

        try {
            MyProgressBarDialogUtil.show(context, R.string.register_geting_verfity_code);
            MyThread.startNewThread(new Runnable() {
                @Override
                public void run() {
                    try {
                        HashMap<String, Object> PARAMS = new HashMap<String, Object>();
                        PARAMS.put("phoneNumber", phone);
                        PARAMS.put("appId", APPID);
                        Log.i(TAG, "GetSMSVerifyCode params  " + PARAMS.toString());
                        String resultStr = WebServiceUtils.callDotNetWS("GetSMSVerifyCode", PARAMS);
                        Log.d(TAG, "GetSMSVerifyCode  resultStr" + resultStr);
                        if (resultStr != null) {
                            try {
                                JSONObject temp = new JSONObject(resultStr);
                                int ret = temp.getInt("ret");
                                int code = temp.getInt("err_code");
                                String msg = temp.getString("msg");
                                if (ret == 0) {
                                    sendMsg(handler, ReErrorCode.ERROR_GET_VERIFY_CODE_SUCCEED, "");
                                } else {
                                    sendMsg(handler, ReErrorCode.ERROR_REGISTER_FAIL, msg);
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                                sendMsg(handler, ReErrorCode.ERRCODE_NO_RETRUN_ERROR, "");
                            }
                        }else{
                            sendMsg(handler, ReErrorCode.ERRCODE_NO_RETRUN_ERROR, "");

                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                        sendMsg(handler, ReErrorCode.ERRCODE_NO_RETRUN_ERROR, "");
                    }
                }
            });

        } catch (Exception e) {
            e.printStackTrace();
            sendMsg(handler, ReErrorCode.ERRCODE_NO_RETRUN_ERROR, "");
        }
    }

    /**
     * 登录时获取用户信息
     */
    public static void GetMemberInfo(final Handler handler, final Context context, final int menberId, final String clientKey) {

        try {
            HashMap<String, Object> params = new HashMap<String, Object>();
            params.put("member_id", menberId);
            params.put("client_key", clientKey);
            params.put("app_id", APPID);


            JSONObject object = new JSONObject(params);
            String infoJson = object.toString();
            HashMap<String, String> PARAMS = new HashMap<String, String>();
            PARAMS.put("dataJson", infoJson);
            Log.i(TAG, "GetMemberInfo params  " + PARAMS.toString());
            String resultStr = WebServiceUtils.callDotNetWS("GetMemberInfo", PARAMS);
            Log.d(TAG, "GetMemberInfo  resultStr" + resultStr);
            if (resultStr != null) {
                try {
                    JSONObject temp = new JSONObject(resultStr);
                    int ret = temp.getInt("ret");
                    int code = temp.getInt("err_code");
                    String msg = temp.getString("msg");
                    if (ret == 0) {
                        JSONObject jsonObject = temp.getJSONObject("data");
                        JSONObject info = jsonObject.getJSONObject("info");
                        MySharedPreferencesUtil.setName(context,info.getString("name"));
                        MySharedPreferencesUtil.setPhotoUrl(context,info.getString("avatar"));
                        MySharedPreferencesUtil.setFamilyId(context,info.getInt("family_id"));
                        FamilyDTO dto = new FamilyDTO();
                        dto.setMember_id(jsonObject.getInt("id"));
                        dto.setFamily_id(info.getInt("family_id"));
                        dto.setBirthday(info.getString("birthday"));
                        dto.setName(info.getString("name"));
                        dto.setAvatar(info.getString("avatar"));
                        dto.setGender(info.getInt("gender"));

                        FamilyIfc ifc = new FamilyIfcImpl(context);
                        ifc.deleteTable();
                        ifc.insert(dto);

                        sendMsg(handler, ReErrorCode.ERRCODE_SUCEED, "");
                    } else {
                        sendMsg(handler, ReErrorCode.ERRCODE_FAIL, msg);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    sendMsg(handler, ReErrorCode.ERRCODE_NO_RETRUN_ERROR, "");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            sendMsg(handler, ReErrorCode.ERRCODE_NO_RETRUN_ERROR, "");
        }


    }


    /**
     * 注册时获取验证码
     */

    public static void GetSMSVerifyCodeForRegister(final Handler handler, final Context context, final String phone, final String ip) {

        try {
            MyProgressBarDialogUtil.show(context, R.string.register_geting_verfity_code);
            MyThread.startNewThread(new Runnable() {
                @Override
                public void run() {
                    try {
                        HashMap<String, Object> PARAMS = new HashMap<String, Object>();
                        PARAMS.put("phoneNumber", phone);
                        PARAMS.put("ip", ip);
                        PARAMS.put("appId", APPID);
                        Log.i(TAG, "GetSMSVerifyCodeForRegister params  " + PARAMS.toString());
                        String resultStr = WebServiceUtils.callDotNetWS("GetSMSVerifyCodeForRegister", PARAMS);
                        Log.d(TAG, "GetSMSVerifyCodeForRegister  resultStr" + resultStr);
                        if (resultStr != null) {
                            try {
                                JSONObject temp = new JSONObject(resultStr);
                                int ret = temp.getInt("ret");
                                int code = temp.getInt("err_code");
                                String msg = temp.getString("msg");
                                if (ret == 0) {
                                    sendMsg(handler, ReErrorCode.ERROR_GET_VERIFY_CODE_SUCCEED, "");
                                } else {
                                    sendMsg(handler, ReErrorCode.ERROR_REGISTER_FAIL, msg);
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                                sendMsg(handler, ReErrorCode.ERRCODE_NO_RETRUN_ERROR, "");
                            }
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                        sendMsg(handler, ReErrorCode.ERRCODE_NO_RETRUN_ERROR, "");
                    }
                }
            });

        } catch (Exception e) {
            e.printStackTrace();
            sendMsg(handler, ReErrorCode.ERRCODE_NO_RETRUN_ERROR, "");
        }
    }


    /**
     * 检验注册验证码
     */
    public static void CheckSMSVerifyCodeForRegister(final Handler handler, final String phone, final String verifyCode) {
        try {

            MyThread.startNewThread(new Runnable() {
                @Override
                public void run() {
                    try {
                        HashMap<String, Object> PARAMS = new HashMap<String, Object>();
                        PARAMS.put("phoneNumber", phone);
                        PARAMS.put("verifyCode", verifyCode);
                        Log.d(TAG, "CheckSMSVerifyCodeForRegister PARAMS " + PARAMS.toString());
                        String resultStr = WebServiceUtils.callDotNetWS("CheckSMSVerifyCodeForRegister", PARAMS);
                        Log.d(TAG, "CheckSMSVerifyCodeForRegister " + resultStr);
                        if (resultStr != null) {
                            try {
                                JSONObject temp = new JSONObject(resultStr);
                                int ret = temp.getInt("ret");
                                String msg = temp.getString("msg");
                                if (ret == 0) {
                                    sendMsg(handler, ReErrorCode.ERRCODE_SUCEED, "");
                                } else {
                                    sendMsg(handler, ReErrorCode.ERROR_REGISTER_FAIL, msg);
                                }

                            } catch (Exception e) {
                                e.printStackTrace();
                                sendMsg(handler, ReErrorCode.ERRCODE_NO_RETRUN_ERROR, "");
                            }
                        }

                    } catch (Exception e) {
                        e.printStackTrace();
                        sendMsg(handler, ReErrorCode.ERRCODE_NO_RETRUN_ERROR, "");
                    }

                }
            });

        } catch (Exception e) {
            e.printStackTrace();
            sendMsg(handler, ReErrorCode.ERRCODE_NO_RETRUN_ERROR, "");
        }


    }


    /**
     * 提交注册信息
     */
    public static void Register(final Handler handler, final Context context, final String phone, final String verifyCode, final String password, final String type) {
        try {
            MyProgressBarDialogUtil.show(context, R.string.set_password_saving);
            MyThread.startNewThread(new Runnable() {
                @Override
                public void run() {
                    try {
                        HashMap<String, Object> params = new HashMap<String, Object>();

                        params.put("type", type);
                        params.put("phone", phone);
                        params.put("verify_code", verifyCode);
                        params.put("password", password);
                        params.put("appid", APPID);


                        JSONObject object = new JSONObject(params);
                        String infoJson = object.toString();
                        HashMap<String, String> PARAMS = new HashMap<String, String>();
                        PARAMS.put("dataJson", infoJson);

                        Log.d(TAG, "Register PARAMS " + PARAMS.toString());
                        String resultStr = WebServiceUtils.callDotNetWS("Register", PARAMS);
                        Log.d(TAG, "Register resultStr " + resultStr);
                        if (resultStr != null) {
                            try {
                                JSONObject temp = new JSONObject(resultStr);
                                int ret = temp.getInt("ret");
                                String msg = temp.getString("msg");
                                if (ret == 0) {
                                    sendMsg(handler, ReErrorCode.ERRCODE_SUCEED, "");
                                } else {
                                    sendMsg(handler, ReErrorCode.ERROR_REGISTER_FAIL, msg);
                                }

                            } catch (Exception e) {
                                e.printStackTrace();
                                sendMsg(handler, ReErrorCode.ERRCODE_NO_RETRUN_ERROR, "");
                            }
                        }

                    } catch (Exception e) {
                        e.printStackTrace();
                        sendMsg(handler, ReErrorCode.ERRCODE_NO_RETRUN_ERROR, "");
                    }

                }
            });

        } catch (Exception e) {
            e.printStackTrace();
            sendMsg(handler, ReErrorCode.ERRCODE_NO_RETRUN_ERROR, "");
        }


    }


    /**
     * 找回密码时，获取验证码
     */
    public static void GetSMSVerifyCodeForForgotPWD(final Handler handler, final Context context, final String phone, final String ip) {

        try {
            MyProgressBarDialogUtil.show(context, R.string.register_geting_verfity_code);
            MyThread.startNewThread(new Runnable() {
                @Override
                public void run() {
                    try {
                        HashMap<String, Object> PARAMS = new HashMap<String, Object>();
                        PARAMS.put("phoneNumber", phone);
                        PARAMS.put("ip", ip);
                        PARAMS.put("appId", APPID);
                        Log.i(TAG, "GetSMSVerifyCodeForForgotPWD  params  " + PARAMS.toString());
                        String resultStr = WebServiceUtils.callDotNetWS("GetSMSVerifyCodeForForgotPWD", PARAMS);
                        Log.d(TAG, "GetSMSVerifyCodeForForgotPWD   resultStr" + resultStr);
                        if (resultStr != null) {
                            try {
                                JSONObject temp = new JSONObject(resultStr);
                                int ret = temp.getInt("ret");
                                int code = temp.getInt("err_code");
                                String msg = temp.getString("msg");
                                if (ret == 0) {
                                    sendMsg(handler, ReErrorCode.ERROR_GET_VERIFY_CODE_SUCCEED, "");
                                } else {
                                    sendMsg(handler, ReErrorCode.ERROR_REGISTER_FAIL, msg);
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                                sendMsg(handler, ReErrorCode.ERRCODE_NO_RETRUN_ERROR, "");
                            }
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                        sendMsg(handler, ReErrorCode.ERRCODE_NO_RETRUN_ERROR, "");
                    }
                }
            });

        } catch (Exception e) {
            e.printStackTrace();
            sendMsg(handler, ReErrorCode.ERRCODE_NO_RETRUN_ERROR, "");
        }
    }

    /**
     * 找回密码时，校验验证码
     */

    public static void CheckSMSVerifyCodeForForgotPWD(final Handler handler, final String phone, final String verifyCode) {
        try {

            MyThread.startNewThread(new Runnable() {
                @Override
                public void run() {
                    try {
                        HashMap<String, Object> PARAMS = new HashMap<String, Object>();
                        PARAMS.put("phoneNumber", phone);
                        PARAMS.put("verifyCode", verifyCode);
                        Log.d(TAG, "CheckSMSVerifyCodeForForgotPWD PARAMS " + PARAMS.toString());
                        String resultStr = WebServiceUtils.callDotNetWS("CheckSMSVerifyCodeForForgotPWD", PARAMS);
                        Log.d(TAG, "CheckSMSVerifyCodeForForgotPWD " + resultStr);
                        if (resultStr != null) {
                            try {
                                JSONObject temp = new JSONObject(resultStr);
                                int ret = temp.getInt("ret");
                                String msg = temp.getString("msg");
                                if (ret == 0) {
                                    sendMsg(handler, ReErrorCode.ERRCODE_SUCEED, "");
                                } else {
                                    sendMsg(handler, ReErrorCode.ERROR_REGISTER_FAIL, msg);
                                }

                            } catch (Exception e) {
                                e.printStackTrace();
                                sendMsg(handler, ReErrorCode.ERRCODE_NO_RETRUN_ERROR, "");
                            }
                        }

                    } catch (Exception e) {
                        e.printStackTrace();
                        sendMsg(handler, ReErrorCode.ERRCODE_NO_RETRUN_ERROR, "");
                    }

                }
            });

        } catch (Exception e) {
            e.printStackTrace();
            sendMsg(handler, ReErrorCode.ERRCODE_NO_RETRUN_ERROR, "");
        }


    }


    /**
     * 设置新密码
     */
    public static void ChangePasswordForForgotPWD(final Handler handler, final Context context, final String phone, final String verifyCode, final String password) {
        try {
            MyProgressBarDialogUtil.show(context, R.string.set_password_saving);
            MyThread.startNewThread(new Runnable() {
                @Override
                public void run() {
                    try {
                        HashMap<String, Object> params = new HashMap<String, Object>();
                        params.put("phoneNumber", phone);
                        params.put("verifyCode", verifyCode);
                        params.put("newPassword", password);
                        Log.d(TAG, "ChangePasswordForForgotPWD PARAMS " + params.toString());
                        String resultStr = WebServiceUtils.callDotNetWS("ChangePasswordForForgotPWD", params);
                        Log.d(TAG, "ChangePasswordForForgotPWD resultStr " + resultStr);
                        if (resultStr != null) {
                            try {
                                JSONObject temp = new JSONObject(resultStr);
                                int ret = temp.getInt("ret");
                                String msg = temp.getString("msg");
                                if (ret == 0) {
                                    sendMsg(handler, ReErrorCode.ERRCODE_SUCEED, "");
                                } else {
                                    sendMsg(handler, ReErrorCode.ERROR_REGISTER_FAIL, msg);
                                }

                            } catch (Exception e) {
                                e.printStackTrace();
                                sendMsg(handler, ReErrorCode.ERRCODE_NO_RETRUN_ERROR, "");
                            }
                        }

                    } catch (Exception e) {
                        e.printStackTrace();
                        sendMsg(handler, ReErrorCode.ERRCODE_NO_RETRUN_ERROR, "");
                    }

                }
            });

        } catch (Exception e) {
            e.printStackTrace();
            sendMsg(handler, ReErrorCode.ERRCODE_NO_RETRUN_ERROR, "");
        }


    }


    /**
     * 上传头像
     */
    public static void UploadPhoto(final Handler handler, final Context context, final String fileName, final Bitmap bitmap, final FamilyDTO familyDTO) {
        try {
            MyProgressBarDialogUtil.show(context,R.string.me_profilr_upload_photo);
            MyThread.startNewThread(new Runnable() {
                @Override
                public void run() {
                    if (bitmap != null) {
                        try {
                            FileInputStream in = new FileInputStream(MyBitmapUtil.PHOTO_DIR + "/" + fileName);
                            byte buffer[] = StreamUtil.read(in);
                            byte[] encode = Base64.encode(buffer, Base64.DEFAULT);

                            HashMap<String, Object> PARAMS_PHOTO = new HashMap<String, Object>();
                            PARAMS_PHOTO.put("memberId", MySharedPreferencesUtil.getMemberId(context));
                            PARAMS_PHOTO.put("clientKey", MySharedPreferencesUtil.getCilenKey(context));
                            PARAMS_PHOTO.put("photoStream", new String(encode));
                            Log.e(TAG, "UploadPhoto    " + PARAMS_PHOTO.toString());
                            String resultStr = WebServiceUtils.callDotNetWS("UploadPhoto", PARAMS_PHOTO);
                            Log.e(TAG, "UploadPhoto    " + resultStr);
                            if (resultStr != null) {
                                try {
                                    JSONObject temp = new JSONObject(resultStr);
                                    int err_code = temp.getInt("err_code");
                                    String msg = temp.getString("msg");
                                    if (err_code == 0) {
                                        Bitmap bitmap1;
                                        JSONObject jsonObject = temp.getJSONObject("data");//上传成功后直接下载回本地
                                        bitmap1  = new DownPhotoUtil().downloadPhotoAva(MySharedPreferencesUtil.getMemberId(context),jsonObject.getString("url"));
                                        MySharedPreferencesUtil.setPhotoUrl(context, jsonObject.getString("url"));
                                        sendMsg(handler, ReErrorCode.ERRCODE_SUCEED, "");
                                        SetMemberInfo(handler,context,MySharedPreferencesUtil.getName(context),MySharedPreferencesUtil.getPhotoUrl(context));
                                    } else {
                                        sendMsg(handler, ReErrorCode.ERRCODE_FAIL, msg);
                                    }


                                } catch (Exception e) {
                                    e.printStackTrace();
                                    sendMsg(handler, ReErrorCode.ERRCODE_NO_RETRUN_ERROR, "");
                                }
                            }else {
                                sendMsg(handler, ReErrorCode.ERRCODE_NO_RETRUN_ERROR, "");
                            }

                        } catch (FileNotFoundException e) {
                            e.printStackTrace();
                        } catch (Exception e) {
                            e.printStackTrace();
                            sendMsg(handler, ReErrorCode.ERRCODE_NO_RETRUN_ERROR, "");

                        }

                    }

                }
            });
        } catch (Exception e) {
            e.printStackTrace();
            sendMsg(handler, ReErrorCode.ERRCODE_NO_RETRUN_ERROR, "");

        }


    }

public  static final  void SetMemberInfo(final Handler handler, final Context context,final String name,final String avatar){
    try {

        MyThread.startNewThread(new Runnable() {
            @Override
            public void run() {

                    try {


                        HashMap<String, Object> PARAMS_PHOTO = new HashMap<String, Object>();
                        HashMap<String,Object> param = new HashMap<String, Object>();

                        param.put("avatar",avatar);
                        param.put("name",name);
                        param.put("real_name",name);
                        param.put("gender","1");
                        param.put("birthday","1980-02-02");
                        param.put("weight","1");
                        param.put("height","1");
                        param.put("waistline","1");
                        param.put("hipline","1");
                        param.put("phone","1");

                        JSONObject object = new JSONObject(param);
                        String infoJson = object.toString();
                        HashMap<String, String> PARAMS = new HashMap<String, String>();


                        PARAMS_PHOTO.put("memberId", MySharedPreferencesUtil.getMemberId(context));
                        PARAMS_PHOTO.put("clientKey", MySharedPreferencesUtil.getCilenKey(context));
                        PARAMS_PHOTO.put("infoJson",infoJson);

                        Log.e(TAG, "SetMenberInfo_Params    " + PARAMS_PHOTO.toString());
                        String resultStr = WebServiceUtils.callDotNetWS("SetMemberInfo", PARAMS_PHOTO);
                        Log.e(TAG, "SetMemberInfo    " + resultStr);
                        if (resultStr != null) {
                            try {
                                JSONObject temp = new JSONObject(resultStr);
                                int err_code = temp.getInt("err_code");
                                String msg = temp.getString("msg");
                                if (err_code == 0) {
                                    MySharedPreferencesUtil.setName(context,name);
                                    MySharedPreferencesUtil.setPhotoUrl(context,avatar);
                                    FamilyDTO dto = new FamilyDTO();
                                    dto.setFamily_id(MySharedPreferencesUtil.getFamilyId(context));
                                    dto.setName(MySharedPreferencesUtil.getName(context));
                                    dto.setAvatar(MySharedPreferencesUtil.getPhotoUrl(context));
                                    FamilyIfc ifc = new FamilyIfcImpl(context);
                                    ifc.updateFamily(dto);
                                    sendMsg(handler, ReErrorCode.ERRCODE_SUCEED, "");

                                } else {
                                    sendMsg(handler, ReErrorCode.ERRCODE_FAIL, msg);
                                }


                            } catch (Exception e) {
                                e.printStackTrace();
                                sendMsg(handler, ReErrorCode.ERRCODE_NO_RETRUN_ERROR, "");
                            }
                        }else {
                            sendMsg(handler, ReErrorCode.ERRCODE_NO_RETRUN_ERROR, "");
                        }

                    } catch (Exception e) {
                        e.printStackTrace();
                        sendMsg(handler, ReErrorCode.ERRCODE_NO_RETRUN_ERROR, "");

                    }

                }

        });
    } catch (Exception e) {
        e.printStackTrace();
        sendMsg(handler, ReErrorCode.ERRCODE_NO_RETRUN_ERROR, "");

    }
}




    /**
     * 用户个人信息
     *
     * @param
     * @param
     * @return
     */
//    public static String user2String2(Context context, FamilyDTO dto) {
//        Map<String, Object> map = new HashMap<String, Object>();
//        map.put("name", dto.getName());
//        map.put("avatar", dto.getAvatar());
//        map.put("birthday", dto.getBirthday());
//        map.put("gender", dto.getGender());
//        JSONObject object = new JSONObject(map);
//        String infoJson = object.toString();
//        Log.v("user2String", "infoJson=" + infoJson);
//        return infoJson;
//    }
    private static HashMap<String, Object> reMap(HashMap<String, Object> param) {
        JSONObject object = new JSONObject(param);
        String dataJson = object.toString();
        HashMap<String, Object> dataMap = new HashMap<String, Object>();
        dataMap.put("dataJson", dataJson);
        Log.d("test", "dataMap::" + dataMap.toString());
        return dataMap;
    }


    public static void sendMsg(Handler handler, int code, String msg) {
        Message message = Message.obtain();
        message.what = code;
        message.obj = msg;
        if (handler != null)
            handler.sendMessage(message);
    }

    public static void sendMsg(Handler handler, int code, Object obj) {
        Message message = Message.obtain();
        message.what = code;
        message.obj = obj;
        if (handler != null)
            handler.sendMessage(message);
    }

    public static void sendMsg(Handler handler, int code, Object obj, int number) {
        Message message = Message.obtain();
        message.what = code;
        message.obj = obj;
        message.arg1 = number;
        if (handler != null)
            handler.sendMessage(message);
    }
}
